﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;

namespace Framework.Common.Sql
{
    public sealed class DbFactory
    {
        private readonly string connStr;
        private readonly DbProviderFactory providerFactory;

        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="factory"></param>  
        public DbFactory(string connectionString, DbProviderFactory factory)
        {
            this.connStr = connectionString;
            this.providerFactory = factory;
            if (this.providerFactory == null)
            {
                throw new ArgumentException("Can't load DbProviderFactory for given value of providerType");
            }
        }
        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="connectionString">数据库连接字符串</param>  
        /// <param name="providerType">数据库类型枚举，参见<paramref name="providerType"/></param>  
        public DbFactory(string connectionString, DbProviderType providerType)
            : this(connectionString, ProviderFactory.GetDbProviderFactory(providerType))
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="setting"></param>
        public DbFactory(ConnectionStringSettings setting)
            : this(setting.ConnectionString, ProviderFactory.GetDbProviderFactory(setting.ProviderName))
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IDbConnection Connection()
        {
            DbConnection connection = providerFactory.CreateConnection();
            if (connection == null)
            {
                throw new Exception("providerFactory.CreateConnection cannot be null");
            }
            connection.ConnectionString = connStr;
            return connection;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="providerType"></param>
        /// <returns></returns>
        public static IDbConnection ByConnectionString(string connectionString, DbProviderType providerType)
        {
            return new DbFactory(connectionString, providerType).Connection();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IDbConnection ByConnectionString(string connectionString)
        {
            // set sqlserver to default DbProvider
            return ByConnectionString(connectionString, DbProviderType.SqlServer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="providerType"></param>
        /// <returns></returns>
        public static IDbConnection ByConfig(string name, DbProviderType providerType = DbProviderType.SqlServer)
        {
            ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings[name];
            if (setting == null)
            {
                string connectionString = ConfigurationManager.AppSettings[name];
                if (connectionString.IsNotBlank())
                {
                    return ByConnectionString(connectionString, providerType);
                }
                throw new Exception("ByConfig(string name) cannot be null");
            }
            else
            {
                return new DbFactory(setting).Connection();
            }
        }
    }


}
